
/**
 * @param {{r: number, g: number, b: number}} c color object having rgb components in the range [0, 255]
 * @returns {number} the luminance as defined by https://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef
 */
function relativeLuminance(c) {
    function f(x) {
        x /= 255;
        return x <= 0.03928 ? (x / 12.92) : Math.pow((x + 0.055) / 1.055, 2.4);
    }
    return 0.2126 * f(c.r) + 0.7152 * f(c.g) + 0.0722 * f(c.b);
}

/**
 * @param {{r: number, g: number, b: number}} c1 color object having rgb components in the range [0, 255]
 * @param {{r: number, g: number, b: number}} c2 color object having rgb components in the range [0, 255]
 * @returns {number} the contrast ratio between the two specified colors, as defined by:
 *          https://www.w3.org/TR/UNDERSTANDING-WCAG20/visual-audio-contrast-contrast#contrast-ratiodef
 */
export function contrastRatio(c1, c2) {
    const l1 = relativeLuminance(c1) + 0.05;
    const l2 = relativeLuminance(c2) + 0.05;
    return Math.max(l1, l2) / Math.min(l1, l2);
}
